# Tools
CXX		= g++
LEX		= flex++
YACCXX		= bisonc++
QMAKE		= qmake

# structure
ROOT		= .
LEXROOT		= $(ROOT)/lex
YACCXXROOT	= $(ROOT)/yaccxx
SRC		= $(ROOT)
BIN		= $(ROOT)/bin

# files
LEXFILES =	$(LEXROOT)/Scanner.l
LEXHEADER =	$(SRC)/Scanner.h
LEXSRC =	$(SRC)/Scanner.cpp

YACCXXFILES =	$(YACCXXROOT)/Parser.y
YACCXXHEADER =	$(SRC)/Parser.h
YACCXXIHEADER =	$(SRC)/Parser.ih
YACCXXSRC =	$(SRC)/Parser.cpp
MOC_FILES = 	$(SRC)/Scanner.h

SOURCES =	$(SRC)/main.cpp \
		$(LEXSRC) \
		$(YACCXXSRC)

# parameters
PROGRAM		= analyzer

CXXFLAGS	= -Wall
YACCXXFLAGS	= --class-header=$(YACCXXHEADER) --implementation-header=$(YACCXXIHEADER) --parsefun-source=$(YACCXXSRC) \
		--verbose --scanner=$(LEXHEADER)
LEXFLAGS	= --nounistd --c++  --header-file=$(LEXHEADER) --outfile=$(LEXSRC)
# -Cfr --verbose

# main rules
all: $(SOURCES) $(BIN)
	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(BIN)/$(PROGRAM)

moc: $(QMAKE) $(MOC_FILES)

prepare: $(SOURCES)

scanner:
	$(LEX) $(LEXFLAGS) $(LEXFILES)

parser:
	$(YACCXX) $(YACCXXFLAGS) $(YACCXXFILES)

force-clean: clean
	rm -f $(YACCXXHEADER)

clean:
	rm -f  $(LEXSRC) $(LEXHEADER) $(YACCXXIHEADER) $(YACCXXSRC) Parserbase.h
	rm -f *.o
	rm -rf $(BIN)

# utility rules
$(SOURCES): scanner parser

# --scanner-token-function=function-call
# The  scanner  function returning the next token, called from the generated parser’s lex function. A complete function
# call expression should be provided (including a scanner object, if used). This option overrules the d_scanner.yylex()
# call used by default when the %scanner directive is specified. Example:
# 
# 	--scanner-token-function "d_scanner.lex()"

$(BIN):
	if [ ! -d $(BIN) ]; then mkdir $(BIN) ; fi